package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
//@Transactional
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    //操作MongoDB数据库
    @Autowired
    private StrategyCommentRepository strategyCommentRepository;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private MongoTemplate mongoTemplate;



    @Override
    public void save(StrategyComment strategyComment) {
        /**
         * 需要维护的字段
         * city(城市)
         * createTime(评论时间)
         * id保证为空
         *
         */
        strategyComment.setId(null);
        strategyComment.setCity(strategyService.findByOne(strategyComment.getStrategyId()).getDestName());
        strategyComment.setCreateTime(new Date());
        strategyCommentRepository.save(strategyComment);
    }

    @Override
    public void delete(String id) {
        strategyCommentRepository.deleteById(id);
    }

    @Override
    public void update(StrategyComment strategyComment) {
        strategyCommentRepository.save(strategyComment);
    }

    

    /**
     * 查询指定攻略明细评论
     * @param qo
     * @return
     */
    @Override
    public Object query(StrategyCommentQuery qo) {
        AssertUtil.hasLength("您查询的文章不存在",qo.getStrategyId());
        //创建查询对象
        Query query = new Query();
        //总条数
        long count = mongoTemplate.count(query, StrategyComment.class);
        if (count==0) {
            //假设查询条数为0,返回空内容
            return Page.empty();
        }

        //查询指定文章攻略评论
        query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        
        count = mongoTemplate.count(query, StrategyComment.class);
        //获取分页对象
        PageRequest pageRequest = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.DESC, "createTime");

        query.with(pageRequest);
        //查询数据库(MongoDB)
        List<StrategyComment> list = mongoTemplate.find(query, StrategyComment.class);
        return new PageImpl<>(list,pageRequest,count);
    }

    @Override
    public StrategyComment findByOne(String id) {
        return strategyCommentRepository.findById(id).orElse(null);
    }

    @Override
    public List<StrategyComment> findAll() {
        return strategyCommentRepository.findAll();
    }

    /**
     * 点赞功能
     * @param uid
     * @param cid
     */
    @Override
    public void commentThumb(String cid, String uid) {
        StrategyComment strategyComment = this.findByOne(cid);
        List<String> thumbuplist = strategyComment.getThumbuplist();
        //判断有没有存在点赞id集合中
        if (thumbuplist.contains(uid)) {
            //存在,取消点赞操作,并移除该id
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()-1);
            thumbuplist.remove(uid);
        } else {
            //不存,点赞操作,添加该id
            strategyComment.setThumbupnum(strategyComment.getThumbupnum()+1);
            thumbuplist.add(uid);
        }
        this.update(strategyComment);

    }

}
